package com.derbysoft.dswitch.adapter.ihg.web.action.admin.setting.rateplan;

import com.derbysoft.dswitch.adapter.ihg.common.domain.RatePlan;
import com.derbysoft.dswitch.adapter.ihg.common.repository.RatePlanRepository;
import com.derbysoft.dswitch.adapter.ihg.web.action.DMXAbstractAction;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * User: jason
 * Date: 2013-02-01
 */
public class DMXAction extends DMXAbstractAction {

    @Autowired
    private RatePlanRepository ratePlanRepository;

    private String ratePlanCode;

    private RatePlan ratePlan;

    @Action("dmxQueryRatePlans")
    public String dmxQueryAll() {
        try {
            ratePlanRepository.paginater(getPaginater());
            return populateSucceededJsonResult(dmxPaging());
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    @Action("dmxQueryRatePlan")
    public String dmxQueryRatePlan() {
        try {
            if (StringUtils.isBlank(ratePlanCode)) {
                return populateSucceededJsonResult("");
            }
            RatePlan ratePlan = ratePlanRepository.loadByCode(StringUtils.upperCase(StringUtils.trim(ratePlanCode)));
            if (ratePlan == null) {
                return populateSucceededJsonResult("");
            }
            return populateSucceededJsonResult(ratePlan);
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    @Action("dmxUpdateRatePlan")
    public String dmxUpdateRatePlan() {
        return saveRatePlan();
    }

    @Action("dmxSaveRatePlan")
    public String dmxSaveRatePlan() {
        return saveRatePlan();
    }

    @Action("dmxDeleteRatePlan")
    public String dmxDeleteRatePlan() {
        try {
            if (StringUtils.isBlank(ratePlanCode)) {
                return populateSucceededJsonResult("");
            }
            RatePlan ratePlan = ratePlanRepository.loadByCode(StringUtils.upperCase(StringUtils.trim(ratePlanCode)));
            if (ratePlan == null) {
                return populateSucceededJsonResult("");
            }
            ratePlanRepository.delete(ratePlan);
            return populateSucceededJsonResult(ratePlan);
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    private String saveRatePlan() {
        try {
            if (ratePlan == null) {
                return populateSucceededJsonResult("");
            }
            ratePlanRepository.save(ratePlan);
            return populateSucceededJsonResult(ratePlan);
        } catch (Exception ex) {
            return populateFailedJsonResult(ex);
        }
    }

    public String getRatePlanCode() {
        return ratePlanCode;
    }

    public void setRatePlanCode(String ratePlanCode) {
        this.ratePlanCode = ratePlanCode;
    }

    public RatePlan getRatePlan() {
        return ratePlan;
    }

    public void setRatePlan(RatePlan ratePlan) {
        this.ratePlan = ratePlan;
    }
}
